package com.developteam.xnmall;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.developteam.xnmall.dao.ProductMapper;
import com.developteam.xnmall.entity.Product4ES;
import com.developteam.xnmall.entity.ProductSku;
import com.developteam.xnmall.entity.ProductVO;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.IOException;
import java.util.List;

/**
 * Description:
 * Author: ZhangXirong 27216
 * Version: 1.0
 * Create Date Time: 2022/3/23 23:34.
 * Update Date Time:
 *
 * @see
 */

@RunWith(SpringRunner.class)
@SpringBootTest(classes = ApiApplication.class)
public class ImportProductInfoIntoES {

    @Autowired
    public RestHighLevelClient restHighLevelClient;
    @Autowired
    public ProductMapper productMapper;
    @Autowired
    public ObjectMapper objectMapper;


    @Test
    public void testCreateIndex() throws IOException {    ///创建索引库保存商品信息

        CreateIndexRequest createIndexRequest = new CreateIndexRequest("fmmallproductsindex");
        CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
        System.out.println(createIndexResponse.isAcknowledged()); ///为true则创建成功
    }

    @Test
    public void testImportData() throws IOException {
//        1. 查询数据  ———— 商品sku（product_sku），价格(product)，图片(product_img)， ES中只存储页面中需要的数据，按照页面所需的规则
        List<ProductVO> productVOS = productMapper.selectProducts();
        System.out.println(productVOS.size());

//        2. 将查询到的数据写入到ES, 按照页面所需的规则, 遍历获取需要的
        for (int i = 0; i < productVOS.size(); i++) {
            ProductVO productVO = productVOS.get(i);

            String productId = productVO.getProductId();
            String productName = productVO.getProductName();
            Integer soldNum = productVO.getSoldNum();

            List<ProductSku> productSkus = productVO.getSkus();
            String skuName = productSkus.size() == 0 ? "" : productSkus.get(0).getSkuName();  ///第一个套餐名称
            String skuImg = productSkus.size() == 0 ? "" : productSkus.get(0).getSkuImg();
            Integer sellPrice = productSkus.size() == 0 ? 0 : productSkus.get(0).getSellPrice();

            Product4ES product = new Product4ES(productId, productName, skuImg, soldNum, skuName, sellPrice);

            //将对象存到 ES 中去
            IndexRequest request = new IndexRequest("fmmallproductsindex");
            request.id(productId);
            request.source(objectMapper.writeValueAsString(product), XContentType.JSON);
            IndexResponse indexResponse = restHighLevelClient.index(request, RequestOptions.DEFAULT);
            System.out.println((i+1) + "----" + indexResponse);




        }


    }

}
